package com.zly.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class LoginInterceptor extends AbstractInterceptor {

	/**
	 * 实例化
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		System.out.println("进入拦截器");
		// ActionContext actionContext = actionInvocation.getInvocationContext();
		// HttpServletRequest request= (HttpServletRequest)
		// actionContext.get(StrutsStatics.HTTP_REQUEST);
		// 获取session判断是否登录
		HttpSession session = ServletActionContext.getRequest().getSession();
		String name = (String) session.getAttribute("name");
		// String name = request.getParameter("name");
		if (null != name && !"".equals(name)) {
			System.out.println("登录成功!" + name);
			return actionInvocation.invoke();
		} else {
			System.out.println("name:" + name);
			System.out.println("非法登录!");
			return "login";
		}
	}

}
